//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.base;

import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.HibernateException;
import org.hibernate.transform.ResultTransformer;

public class BeanTransformer implements ResultTransformer {
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private Class clazz;
    private Map<String, Method> methodMap = new HashMap();

    public BeanTransformer(Class clazz) {
        this.clazz = clazz;
    }

    public Object transformTuple(Object[] tuple, String[] aliases) {
        try {
            if(aliases.length != 1 || !this.clazz.getName().startsWith("java.lang") && !this.clazz.equals(Date.class)) {
                Object e = this.clazz.newInstance();

                for(int i = 0; i < aliases.length; ++i) {
                    Method method = (Method)this.methodMap.get(aliases[i]);
                    if(method == null) {
                        method = this.findMethod(aliases[i]);
                        if(method == null) {
                            throw new Exception(aliases[i] + " can\'t find");
                        }
                    }

                    if(tuple[i] != null) {
                        method.invoke(e, new Object[]{this.getObject(method.getParameterTypes()[0], tuple[i])});
                    }
                }

                return e;
            } else {
                return this.getObject(this.clazz, tuple[0]);
            }
        } catch (Exception var6) {
            throw new HibernateException(var6.getMessage());
        }
    }

    public List transformList(List list) {
        return list;
    }

    private Method findMethod(String alias) {
        Method method = null;
        Method[] methods = this.clazz.getMethods();
        Method[] arr$ = methods;
        int len$ = methods.length;

        for(int i$ = 0; i$ < len$; ++i$) {
            Method m = arr$[i$];
            if(m.getName().startsWith("set") && m.getParameterTypes().length == 1) {
                String ss = alias.indexOf("_") > 0?alias.replaceAll("_", ""):alias;
                if(m.getName().substring(3).equalsIgnoreCase(ss)) {
                    method = m;
                    break;
                }
            }
        }

        if(method != null) {
            this.methodMap.put(alias, method);
        }

        return method;
    }

    private Object getObject(Class clazz, Object value) throws Exception {
        if(value == null) {
            return null;
        } else {
            Object obj = null;
            if(clazz.equals(String.class)) {
                obj = String.valueOf(value);
            } else if(!clazz.equals(Integer.class) && !clazz.getName().equalsIgnoreCase("int")) {
                if(!clazz.equals(Long.class) && !clazz.getName().equalsIgnoreCase("long")) {
                    if(!clazz.equals(Float.class) && !clazz.getName().equalsIgnoreCase("float")) {
                        if(!clazz.equals(Double.class) && !clazz.getName().equalsIgnoreCase("double")) {
                            if(clazz.equals(Date.class)) {
                                if(value instanceof String) {
                                    obj = this.sdf.parse(value.toString());
                                } else {
                                    obj = value;
                                }
                            } else {
                                obj = value;
                            }
                        } else if(value instanceof Number) {
                            obj = Double.valueOf(((Number)value).doubleValue());
                        } else {
                            obj = Double.valueOf(Double.parseDouble(value.toString()));
                        }
                    } else if(value instanceof Number) {
                        obj = Float.valueOf(((Number)value).floatValue());
                    } else {
                        obj = Float.valueOf(Float.parseFloat(value.toString()));
                    }
                } else if(value instanceof Number) {
                    obj = Long.valueOf(((Number)value).longValue());
                } else {
                    obj = Long.valueOf(Long.parseLong(value.toString()));
                }
            } else if(value instanceof Number) {
                obj = Integer.valueOf(((Number)value).intValue());
            } else {
                obj = Integer.valueOf(Integer.parseInt(value.toString()));
            }

            return obj;
        }
    }
}
